<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格" prop="goodsGg">
        <el-input
          v-model="queryParams.goodsGg"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品单位" prop="goodsUnit">
        <el-input
          v-model="queryParams.goodsUnit"
          placeholder="请输入商品单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="goodsPrice">
        <el-input
          v-model="queryParams.goodsPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单件瓶碗数" prop="goodsUnitPiece">
        <el-input
          v-model="queryParams.goodsUnitPiece"
          placeholder="请输入单件瓶碗数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安全库存" prop="goodsSaveStore">
        <el-input
          v-model="queryParams.goodsSaveStore"
          placeholder="请输入安全库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提前预订天数" prop="goodsCheckDay">
        <el-input
          v-model="queryParams.goodsCheckDay"
          placeholder="请输入提前预订天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="商品编码" align="center" prop="goodsCode" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品规格" align="center" prop="goodsGg" />
      <el-table-column label="商品单位" align="center" prop="goodsUnit" />
      <el-table-column label="单价" align="center" prop="goodsPrice" />
      <el-table-column label="单件瓶碗数" align="center" prop="goodsUnitPiece" />
      <el-table-column label="安全库存" align="center" prop="goodsSaveStore" />
      <el-table-column label="提前预订天数" align="center" prop="goodsCheckDay" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电商商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">


        <el-form-item label="商品编码" prop="goodsCode">
          <el-input v-model="form.goodsCode" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsGg">
          <el-input v-model="form.goodsGg" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="商品单位" prop="goodsUnit">
          <el-select v-model="form.goodsUnit" placeholder="请输入商品单位">
            <el-option value="PCS">PCS</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="单件瓶碗数" prop="goodsUnitPiece">
          <el-input v-model="form.goodsUnitPiece" placeholder="请输入单件瓶碗数" />
        </el-form-item>
        <el-form-item label="安全库存" prop="goodsSaveStore">
          <el-input v-model="form.goodsSaveStore" placeholder="请输入安全库存" />
        </el-form-item>
        <el-form-item label="提前预订天数" prop="goodsCheckDay">
          <el-input v-model="form.goodsCheckDay" placeholder="请输入提前预订天数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="关联物料" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请选择物料" readonly>
            <el-button slot="append" icon="el-icon-search" @click="openItemDialog"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="物料名称" prop="itemName">
          <el-input v-model="form.itemName" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 物料选择对话框 -->
    <el-dialog title="选择物料" :visible.sync="itemDialogVisible" width="800px" append-to-body>
      <div style="height: 500px; overflow-y: auto;">
        <el-form :model="itemQueryParams" ref="itemQueryForm" size="small" :inline="true">
          <el-form-item label="物料编码">
            <el-input v-model="itemQueryParams.itemCode" placeholder="请输入物料编码" clearable />
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input v-model="itemQueryParams.itemName" placeholder="请输入物料名称" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchItemList">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-table
            v-loading="itemLoading"
            :data="itemList"
            @row-dblclick="selectItem"
            highlight-current-row
            height="300"
            border>
          <el-table-column prop="itemCode" label="物料编码" width="150"></el-table-column>
          <el-table-column prop="itemName" label="物料名称" width="200"></el-table-column>
          <el-table-column prop="specification" label="规格型号"></el-table-column>
          <el-table-column prop="unitOfMeasure" label="单位" width="80"></el-table-column>
          <el-table-column label="操作" width="80">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="selectItem(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            v-show="itemTotal>0"
            :total="itemTotal"
            :page.sync="itemQueryParams.pageNum"
            :limit.sync="itemQueryParams.pageSize"
            @pagination="searchItemList"
        />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="itemDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/shop/goods";
import { listMdItem } from "@/api/mes/md/mdItem";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电商商品管理表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       // 物料列表
      itemList: [],
      // 物料搜索加载状态
      itemLoading: false,
      // 物料选择弹窗可见性
          itemDialogVisible: false,
          // 物料查询参数
          itemQueryParams: {
            pageNum: 1,
            pageSize: 10,
            itemCode: null,
            itemName: null
          },
          // 物料总数
          itemTotal: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsCode: null,        goodsName: null,        goodsGg: null,        goodsUnit: null,        goodsPrice: null,        goodsUnitPiece: null   ,itemCode: null,itemName: null   },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsCode: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        goodsGg: [
          { required: true, message: "商品规格不能为空", trigger: "blur" }
        ],
        goodsUnit: [
          { required: true, message: "商品单位不能为空", trigger: "blur" }
        ],
        goodsPrice: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        goodsUnitPiece: [
          { required: true, message: "单件瓶碗数不能为空", trigger: "blur" }
        ],
        goodsSaveStore: [
          { required: true, message: "单件瓶碗数不能为空", trigger: "blur" }
        ],
        goodsCheckDay: [
          { required: true, message: "单件瓶碗数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电商商品管理列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        remark: null,        status: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        goodsCode: null,        goodsName: null,        goodsGg: null,        goodsUnit: null,        goodsPrice: null,        goodsUnitPiece: null   ,itemCode: null,itemName: null   };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电商商品管理";
      this.form.goodsUnit = 'PCS'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电商商品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电商商品管理编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
         // 打开物料选择弹窗
          openItemDialog() {
            this.itemDialogVisible = true;
            this.itemQueryParams = {
              pageNum: 1,
              pageSize: 10,
              itemCode: null,
              itemName: null
            };
            this.searchItemList();
          },

          // 搜索物料列表
          searchItemList() {
            this.itemLoading = true;
            listMdItem(this.itemQueryParams).then(response => {
              this.itemList = response.rows;
              this.itemTotal = response.total;
              this.itemLoading = false;
            }).catch(() => {
              this.itemLoading = false;
            });
          },

          // 选择物料
          selectItem(row) {
            this.form.itemCode = row.itemCode;
            this.form.itemName = row.itemName;
            this.itemDialogVisible = false;
          },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
